package com.carkeeper.user.service.impl;

import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.user.dao.DepartmentMapper;
import com.carkeeper.user.pojo.Department;
import com.carkeeper.user.service.DepartmentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * 查询全部数据
     * @return
     */
    @Override
    public List<Department> findAll() {
        List<Department> departmentList = departmentMapper.findAll();
        return departmentList;
    }

    /***
     * 新增数据
     * @param department
     * @return
     */
    @Override
    public void add(Department department) {
        department.setCreateTime(new Date());
       departmentMapper.insert(department);

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Department findById(Integer id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    /***
     * 修改数据
     * @param department
     * @return
     */
    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);

    }

    /***
     * 根据ID删除品牌数据(逻辑删除)
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        Department department = new Department();
        Integer delet = department.getDelet();
        delet= departmentMapper.deleteByPrimaryKey(id);
        if (0==delet){
            //部门未被删除
            department.setDelet(1);
            departmentMapper.updateByPrimaryKeySelective(department);
            return new Result(true, StatusCode.OK,"删除成功");
        }else {
            return new Result(false,StatusCode.ERROR,"该部门已被删除");
        }
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Department> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Department>) departmentMapper.selectAll();
    }
    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Department> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        if (example.getOredCriteria().size() == 1) {
            example.clear();
        }
        return (Page<Department>) departmentMapper.selectByExample(example);
    }

/*上面注意if (example.getOredCriteria().size() == 1) {
            example.clear();
        }是因为在实体类里用@logicDelete注解标注了逻辑删除字段它会在你条件查询不带条件时多拼一个and
        SELECT  id,shop_id,name,label  FROM tb_sys_region  WHERE del_flag = 0 and
        原因是底层代码拼了个and:
          public static String andNotLogicDelete(Object parameter) {
        String result = "";
        if (parameter instanceof Example) {
            Example example = (Example) parameter;
            Map<String, EntityColumn> propertyMap = example.getPropertyMap();

            for (Map.Entry<String, EntityColumn> entry: propertyMap.entrySet()) {
                EntityColumn column = entry.getValue();
                if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
                    // 未逻辑删除的条件
                    result = column.getColumn() + " = " + SqlHelper.getLogicDeletedValue(column, false);

                    // 如果Example中有条件，则拼接" and "，
                    // 如果是空的oredCriteria，则where中只有逻辑删除注解的未删除条件
                    if (example.getOredCriteria() != null && example.getOredCriteria().size() != 0) {
                        result += " and ";
                    }
                }
            }
        }
        return result;
    }

最后的判断导致。
如果执行过example.createCriteria()之后，而没有增加任何条件的话，即没有执行过任何的criteria.andEqualTo(XXX)，就会导致这个BUG。

补救办法：
在super.selectByExample(example)之前加上如下逻辑即可：

        if (example.getOredCriteria().size() == 1) {
            example.clear();
        }*/



    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String,Object> searchMap){
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            //部门编号
            if (searchMap.get("code")!=null && !"".equals(searchMap.get("code"))){
                criteria.andEqualTo("code",searchMap.get("code"));
            }
            //部门名称
            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
        }
         return example;
    }
}
